/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.global.resource.ResourceManager;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifTextView;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * GifTextViewTest
 */
public class GifTextViewTest {
    private static final int LOOP_COUNT_0 = 0;
    private static final int LOOP_COUNT_5 = 5;
    private static final int FRAME_COUNT = 4;  // Number of frames in the given gif file

    ComponentContainer rootLayout;
    GifTextView text;
    GifTextView textBackground;
    private Context mContext;

    /**
     * setUp
     */
    @Before
    public void setUp() {
        mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        rootLayout = (ComponentContainer) LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_gif_text_view, null, false);

        ResourceManager resourceManager = mContext.getResourceManager();

        GifDrawable gifDrawable1 = null;
        GifDrawable gifDrawable2 = null;
        GifDrawable gifDrawable3 = null;
        GifDrawable gifDrawable4 = null;
        GifDrawable background = null;
        try {
            gifDrawable1 = new GifDrawable(resourceManager
                .getRawFileEntry("entry/resources/rawfile/anim_flag_england.gif").openRawFileDescriptor());
            gifDrawable2 = new GifDrawable(resourceManager
                .getRawFileEntry("entry/resources/rawfile/anim_flag_chile.gif").openRawFileDescriptor());
            gifDrawable3 = new GifDrawable(resourceManager
                .getRawFileEntry("entry/resources/rawfile/anim_flag_greenland.gif").openRawFileDescriptor());
            gifDrawable4 = new GifDrawable(resourceManager
                .getRawFileEntry("entry/resources/rawfile/anim_flag_iceland.gif").openRawFileDescriptor());
            background = new GifDrawable(resourceManager
                .getRawFileEntry("entry/resources/rawfile/anim_flag_iceland.gif").openRawFileDescriptor());
        } catch (IOException e) {
            // ignore
        }

        text = (GifTextView) rootLayout.findComponentById(ResourceTable.Id_gif);
        text.setElements(gifDrawable1, gifDrawable2, gifDrawable3, gifDrawable4);

        textBackground = (GifTextView) rootLayout.findComponentById(ResourceTable.Id_gif1);
        background.setElementCallback(textBackground);
        textBackground.setBackgroundElement(background);
    }

    /**
     * testLeftElement
     */
    @Test
    public void testLeftElement() {
        assertNotNull(text.getLeftElement());
    }

    /**
     * testTopElement
     */
    @Test
    public void testTopElement() {
        assertNotNull(text.getTopElement());
    }

    /**
     * testRightElement
     */
    @Test
    public void testRightElement() {
        assertNotNull(text.getRightElement());
    }

    /**
     * testBottomElement
     */
    @Test
    public void testBottomElement() {
        assertNotNull(text.getBottonElement());
    }

    /**
     * testBackgroundElement
     */
    @Test
    public void testBackgroundElement() {
        assertNotNull(textBackground.getBackgroundElement());
    }

    /**
     * testLoopCountNotSet
     */
    @Test
    public void testLoopCountNotSet() {
        GifDrawable element = (GifDrawable) text.getLeftElement();
        assertEquals(element.getLoopCount(), LOOP_COUNT_0);
    }

    /**
     * testLoopCountSet
     */
    @Test
    public void testLoopCountSet() {
        text.setLoopCount(LOOP_COUNT_5);
        GifDrawable element = (GifDrawable) text.getRightElement();
        assertEquals(element.getLoopCount(), LOOP_COUNT_5);
    }

    /**
     * testLoopCountNotSetForBackground
     */
    @Test
    public void testLoopCountNotSetForBackground() {
        GifDrawable element = (GifDrawable) textBackground.getBackgroundElement();
        assertEquals(element.getLoopCount(), LOOP_COUNT_0);
    }

    /**
     * testLoopCountSetForBackground
     */
    @Test
    public void testLoopCountSetForBackground() {
        GifDrawable element = (GifDrawable) textBackground.getBackgroundElement();
        element.setLoopCount(LOOP_COUNT_5);
        assertEquals(element.getLoopCount(), LOOP_COUNT_5);
    }

    /**
     * testNumberOfFrames
     */
    @Test
    public void testNumberOfFrames() {
        GifDrawable element = (GifDrawable) textBackground.getBackgroundElement();
        assertEquals(element.getNumberOfFrames(), FRAME_COUNT);
    }
}